package domain;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class Test {
	public static void Translate() throws IOException
	{
		String message = URLEncoder.encode("<a href='dsada' title='go hello'>go</a>[Querying whois.verisign-grs.com] [Redirected to whois.joker.com] [Querying whois.joker.com] [whois.joker.com] domain: pikimal.com owner: Eric Silver email: address: 1492 Chukar Cr. city: Sunnyvale state: CA postal-code: 94087 country: US phone:  1.4087330486 admin-c: CCOM-15136 tech-c: CCOM-15136 billing-c: CCOM-15136 nserver: a.ns.joker.com 207.44.185.100 nserver: b.ns.joker.com 66.197.237.21 nserver: c.ns.joker.com 207.44.185.10 status: lock created: 2006-03-14 13:04:00 UTC modified: 2011-01-27 19:26:35 UTC expires: 2013-03-14 12:00:22 UTC query-source: 184.107.58.9  contact-hdl: CCOM-15136 person: Eric Silver email: address: 700 River Avenue address: #214 city: Pittsburgh state: PA postal-code: 15212 country: US phone:  1.4084806390  source: joker.com live whois service query-time: 0.006946 db-updated: 2011-12-28 07:52:00 NOTE: By submitting a WHOIS query, you agree to abide by the following NOTE: terms of use: You agree that you may use this data only for lawful NOTE: purposes and that under no circumstances will you use this data to: NOTE: (1) allow, enable, or otherwise support the transmission of mass NOTE: unsolicited, commercial advertising or solicitations via direct mail, NOTE: e-mail, telephone, or facsimile; or (2) enable high volume, automated, NOTE: electronic processes that apply to Joker.com (or its computer systems). NOTE: The compilation, repackaging, dissemination or other use of this data NOTE: is expressly prohibited without the prior written consent of Joker.com.", "UTF-8");
		
		URL url = new URL("http://thedomainfo.com/recetas-de-cocina.net/");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 AlexaToolbar/alxf-2.14 AlexaToolbar/pXVUnvuf-1.2 Firefox/3.6.21");
		connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
		connection.setRequestProperty("Accept-Charset", "	ISO-8859-1,utf-8;q=0.7,*;q=0.7");
		connection.setRequestProperty("Connection", "keep-alive");
		connection.setRequestProperty("Keep-Alive", "115");
		connection.setRequestProperty("Cookie", "PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES");
		connection.setRequestProperty("Cache-Control", "max-age=0");
		OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
//        writer.write("text=" + message);
//        writer.write("&eotf=1");
//        writer.write("&hl=vi");
//        writer.write("&ie=UTF-8");
//        writer.write("&js=n");
//        writer.write("&layout=2");
//        writer.write("&prev=_t");
//        writer.write("&sl=en");
//        writer.write("&tl=vi");
        writer.close();
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        	String content = "";			
        	BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
			while(1==1)
			{
				String str = reader.readLine();
				if(str==null) break;
				content+=str;				
			}
			Document doc = Jsoup.parse(content);
			System.out.println(doc);
        }
	}
	public static void main(String[] args) throws IOException, InterruptedException {
//		Translate();
		DBCollection coll = Data.data("domaincraw");
//		System.out.println("The Metropolitan Museum of Art (colloquially The Met) is an art museum in New ..... the seminal Surrealist magazine VVV and a copy of La Description de l'Egypte, .... m2) of floor space, more than 20 times the size of the original 1880 building.".length());
		DBCursor db = coll.find();
		
		
		try{
			  // Create file 
			  FileWriter fstream = new FileWriter("reg.txt");
			  BufferedWriter out = new BufferedWriter(fstream);
			  while(db.hasNext())
			  {
				  DBObject object=db.next();
//				  System.out.println(object.get("domain"));
				  if(object.get("domain").toString().equals("{ \"http://csstips&&&org\" : 1}"))
				  out.write(object.get("url")+"|"+object.get("reg_link")+"|"+object.get("reg_title")+"|"+object.get("reg_content")+"|"+object.get("reg_remove")+"\n");
			  }
			  //Close the output stream
			  out.close();
		}catch (Exception e){//Catch exception if any
			  System.err.println("Error: " + e.getMessage());
		}
		
//		for (int i = 0; i < 100; i++) {
//			Translate();
//		}
	}
}
